package ee.maoinvest.frontend.controller.requestprocessors;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import ee.maoinvest.backend.model.data.ElectricityMeter;
import ee.maoinvest.backend.model.data.RegularReading;
import ee.maoinvest.frontend.controller.formstringdataconverters.regularreadingsforms.SaveElectricityMeterRegularReadingFormConverter;
import ee.maoinvest.frontend.controller.formstringdataconverters.regularreadingsforms.StartNewRegularReadingFormConverter;

public class RegularReadingsFormsRequestProcessor 
{
	public StartNewRegularReadingFormConverter fillStartNewRegularReadingWithRequestData(
			HttpServletRequest request)
	{
		StartNewRegularReadingFormConverter startNewRegularReadingFormConverter = new StartNewRegularReadingFormConverter();
		
		String dateInserted = request.getParameter("dateInserted");
		String latestDateLimitationForDateInserted = request.getParameter("latestDateLimitationForDateInserted");
		
		HttpSession session = request.getSession();
		RegularReading latestRegularReadingTaken = (RegularReading) session.getAttribute("latestRegularReadingTaken");
		
		startNewRegularReadingFormConverter.setDateInserted(dateInserted);
		startNewRegularReadingFormConverter.setLatestDateLimitationForDateInserted(latestDateLimitationForDateInserted);
		startNewRegularReadingFormConverter.setLatestRegularReadingTaken(latestRegularReadingTaken);
		
		return startNewRegularReadingFormConverter;
	}
	
	public SaveElectricityMeterRegularReadingFormConverter fillSaveElectricityMeterRegularReadingFormConverterWithRequestData(
			HttpServletRequest request)
	{
		SaveElectricityMeterRegularReadingFormConverter saveElectricityMeterRegularReadingFormConverter = 
				new SaveElectricityMeterRegularReadingFormConverter();
		
		String electricityMeterIDWhichRegularReadingToSave = request.getParameter("electricityMeterIDWhichRegularReadingToSave");
		String daytimeReading = request.getParameter("daytimeReading");
		String nightTimeReading = request.getParameter("nightTimeReading");
		String reactivePowerReading = request.getParameter("reactivePowerReading");
		
		HttpSession session = request.getSession();
		@SuppressWarnings("unchecked")
		List<ElectricityMeter> listOfAllElectricityMetersTakingPartInRegularReadings = (List<ElectricityMeter>) session.getAttribute(
				"listOfAllElectricityMetersTakingPartInRegularReadings");
		RegularReading regularReading = (RegularReading) session.getAttribute("latestRegularReading");
		
		saveElectricityMeterRegularReadingFormConverter.setElectricityMeterIDWhichRegularReadingToSave(
				electricityMeterIDWhichRegularReadingToSave);
		saveElectricityMeterRegularReadingFormConverter.setDaytimeReading(daytimeReading);
		saveElectricityMeterRegularReadingFormConverter.setNightTimeReading(nightTimeReading);
		saveElectricityMeterRegularReadingFormConverter.setReactivePowerReading(reactivePowerReading);
		saveElectricityMeterRegularReadingFormConverter.setListOfAllElectricityMetersTakingPartInRegularReadings(
				listOfAllElectricityMetersTakingPartInRegularReadings);
		saveElectricityMeterRegularReadingFormConverter.setRegularReading(regularReading);
		
		return saveElectricityMeterRegularReadingFormConverter;
	}
}
